package com.drcoffee.drcoffeemall.mapper;

import com.drcoffee.drcoffeemall.entry.Address;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface AddressMapper {

    /* 添加地址 */
    @Insert("INSERT INTO t_address VALUES (#{id}, #{recipients}, #{area_code}, #{phone}, #{province}, #{city}, #{region}, #{address},#{user_account},#{province_code},#{city_code},#{region_code})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void addAddress(Address address);

    /* 设置默认地址 */
//    @Update("UPDATE t_user SET default_address= #{address_id} WHERE account = #{account}")
//    void setDefaultAddress(@Param("address_id") Integer address_id, @Param("account") String account);

    /* 查询默认地址 */
    @Select("SELECT * FROM t_address WHERE id = #{id}")
    Address getAddressById(Integer id);


    /* 删除地址 */
    @Delete("DELETE FROM t_address WHERE id = #{id}")
    void deleteAddress(Integer id);

    /* 修改地址 */
    void updateAddress(Address address);


    /* 根据账号查询地址 */
//    @Select("SELECT a.* FROM t_address a left join t_user b on a.user_account = b.account WHERE a.user_account = #{account} and a.id != b.default_address ")
    @Select("SELECT * FROM t_address WHERE user_account = #{account}")
    List<Address> getAddressByAccount(String account);

    /* 根据account查询地址数量 */
    @Select("SELECT count(*) FROM t_address WHERE user_account = #{account}")
    Integer getAddressCountByAccount(String account);

}
